//
//  ViewController.m
//  LXLessDemo
//
//  Created by LX on 16/2/10.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "ViewController.h"
#import "LXHomeMoreCell.h"
#import "LXOfficialActivitiesCell.h"
#import "LXHotUserCell.h"
#import "LXEveryoneWatchingCell.h"
#import "LXActivityViewController.h"
#import "LXHomeViewController.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *mainTable;
@property (strong, nonatomic) NSMutableArray *datas;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = backColor;
    _datas = [NSMutableArray array];
    [self settingTestDatfa];
    self.mainTable.dataSource = self;
    self.mainTable.delegate = self;
    self.title = @"发现 Less";
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillAppear:(BOOL)animated{
//    self.mainTable.frame = self.view.frame      ;
    [LXCommonUtils setDefaultTintForNavigationController:self.navigationController];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5 + _datas.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 4) {
        return 52;
    }
    else if (indexPath.row == 1){
        return 100;
    }
    else if (indexPath.row == 3){
        return 111;
    }
    else if (indexPath.row > 4){
        CGFloat h = (CGRectGetWidth([UIScreen mainScreen].bounds) - (3-1)*1)/3;
        return h;
    }

    return 52;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 || indexPath.row == 2) {
        static NSString *MoreCellIndentfer = @"MoreCellIndentfer";
        LXHomeMoreCell *MoreCell = [tableView dequeueReusableCellWithIdentifier:MoreCellIndentfer];
        if (!MoreCell) {
            MoreCell = [[[NSBundle mainBundle] loadNibNamed:@"LXHomeMoreCell" owner:self options:nil] objectAtIndex:0] ;

//            [tableView registerNib:[UINib nibWithNibName:@"LXHomeMoreCell" bundle:nil] forCellReuseIdentifier:MoreCellIndentfer];
//            MoreCell = [tableView dequeueReusableCellWithIdentifier:MoreCellIndentfer];
        }
        NSString *titleString = @"官方活动";
        if (indexPath.row == 2) {
            titleString = @"热门用户";
        }
        [MoreCell settingTitle:titleString];
        __weak typeof(self) weakSelf = self;
        MoreCell.selectBlock = ^(BOOL isSelect){
        };
        return MoreCell;
    }
    else if (indexPath.row == 1){
        static NSString *OfficialActivitiesCellIndentfer = @"OfficialActivitiesCellIndentfer";
        LXOfficialActivitiesCell *OfficialActivitiesCell = [tableView dequeueReusableCellWithIdentifier:OfficialActivitiesCellIndentfer];
        if (!OfficialActivitiesCell) {
            OfficialActivitiesCell = [[[NSBundle mainBundle] loadNibNamed:@"LXOfficialActivitiesCell" owner:self options:nil] objectAtIndex:0] ;
        }
        [OfficialActivitiesCell settingOfficialActivitiesWithDatas:_datas[2]];
        __weak typeof(self) weakSelf = self;
        OfficialActivitiesCell.selectBlock = ^(NSString *objID){
            [weakSelf pushViewCrontrol];
        };
        return OfficialActivitiesCell;
    }
    else if (indexPath.row == 3){
        static NSString *HotUserCellIndentfer = @"HotUserCellIndentfer";
        LXHotUserCell *HotUserCell = [tableView dequeueReusableCellWithIdentifier:HotUserCellIndentfer];
        if (!HotUserCell) {
            HotUserCell = [[[NSBundle mainBundle] loadNibNamed:@"LXHotUserCell" owner:self options:nil] objectAtIndex:0] ;
        }
        NSMutableArray*aa = [NSMutableArray array];
        for (NSArray *arr in _datas) {
            for (NSString *d in arr) {
                if (d.length > 0) {
                    [aa addObject:d];
                }
            }
        }
        [HotUserCell settingHoteUserViewWithDatas:aa];
        __weak typeof(self) weakSelf = self;
        HotUserCell.selectBlock = ^(NSString *selectID){
            [weakSelf pushHomeCrontrol];
        };
        return HotUserCell;
    }
    else if (indexPath.row > 4){
        static NSString *EveryoneWatchingCellIndentfer = @"EveryoneWatchingCellIndentfer";
        LXEveryoneWatchingCell *EveryoneWatchingCell = [tableView dequeueReusableCellWithIdentifier:EveryoneWatchingCellIndentfer];
        if (!EveryoneWatchingCell) {
            EveryoneWatchingCell = [[[NSBundle mainBundle] loadNibNamed:@"LXEveryoneWatchingCell" owner:self options:nil] objectAtIndex:0] ;
        }
        
        [EveryoneWatchingCell settingEveryoneWatchingViewWithList:_datas[indexPath.row - 5]];
        __weak typeof(self) weakSelf = self;
        EveryoneWatchingCell.selectBlock = ^(NSString *selectID){
            [weakSelf pushViewCrontrol];
        };
        return EveryoneWatchingCell;
    }
    static NSString *cellIndentfer = @"ccelldengng";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentfer];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentfer];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.textColor = [UIColor whiteColor];
    if (indexPath.row == 4) {
        cell.textLabel.text = @"大家都在看";
        return cell;
    }

    return cell;
}
-(void)pushHomeCrontrol{
    LXHomeViewController *view = [[LXHomeViewController alloc] init];
    [self.navigationController pushViewController:view animated:YES];

}
-(void)pushViewCrontrol{
    LXActivityViewController *view = [[LXActivityViewController alloc] init];
    [self.navigationController pushViewController:view animated:YES];
    
}

-(void)settingTestDatfa{

    NSMutableArray *att = [NSMutableArray array];
    for (int i = 0; i < 19; i++) {
        for (int j = 0; j < 3; j++) {
            if (i*3 + j > 19) {
                continue;
            }
            NSString *string = [NSString stringWithFormat:@"%d",i*3 + j];
            [att addObject:string];
        }
        if (att.count) {
            [_datas addObject:att];
        }
        if (att.count < 3) {
            break;
        }
        att = [NSMutableArray array];
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}



@end
